package com.ycsj.goldmedalnewconcept.adapter;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;







import java.util.ArrayList;
import java.util.List;

import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.CreateTeamActivity;
import com.ycsj.goldmedalnewconcept.bean.ClassUseInfo;
import com.ycsj.goldmedalnewconcept.bean.PersonInfo;
import com.ycsj.goldmedalnewconcept.utils.GsonUtil;


/**
 * 创建日期：2017/11/9  9:24
 * 描述:班级信息的适配器
 * 作者:张伟
 */

public class ClassInfoAdapter extends CommonAdapter<ClassUseInfo> {

    private Activity activity;
    private Context context;
    private int screenWidth;
    private LinearLayout.LayoutParams lp;
    private List<PersonInfo> personInfos;
    private ClassGvAdapter gvAdapter;

    private List<ClassUseInfo> datas;

    //用于保存小组头像的数组
    private List<Integer> teamIcons = new ArrayList<>();
    //用于保存小组名称的集合
    private List<String> teamNames = new ArrayList<>();


    public ClassInfoAdapter(Context context, List<ClassUseInfo> datas, int layoutId, int screenWidth, Activity activity) {
        super(context, datas, layoutId);
        this.context = context;
        this.screenWidth = screenWidth;
        this.activity = activity;
        this.datas = datas;
        for (int i = 0; i < datas.size(); i++) {
            teamIcons.add(Integer.parseInt(datas.get(i).getIcon()));
            teamNames.add(datas.get(i).getName());
        }
        lp = new LinearLayout.LayoutParams((screenWidth - dp2px(context, 8 * 2) / 4), ViewGroup.LayoutParams.MATCH_PARENT);
    }

    @Override
    public void convert(ViewHolder holder, final ClassUseInfo info) {
        //小组头像的线性布局
        LinearLayout llGlade = holder.getView(R.id.ll_glade);

        //组图片
        ImageView lvGlade = holder.getView(R.id.lv_glade);
        if (info.getIcon().equals("1")) {
            lvGlade.setImageResource(R.drawable.group1);
        } else if (info.getIcon().equals("2")) {
            lvGlade.setImageResource(R.drawable.group2);
        } else if (info.getIcon().equals("3")) {
            lvGlade.setImageResource(R.drawable.group3);
        } else if (info.getIcon().equals("4")) {
            lvGlade.setImageResource(R.drawable.group4);
        } else if (info.getIcon().equals("5")) {
            lvGlade.setImageResource(R.drawable.group5);
        } else if (info.getIcon().equals("6")) {
            lvGlade.setImageResource(R.drawable.group6);
        } else if (info.getIcon().equals("7")) {
            lvGlade.setImageResource(R.drawable.group7);
        } else if (info.getIcon().equals("8")) {
            lvGlade.setImageResource(R.drawable.group8);
        } else if (info.getIcon().equals("9")) {
            lvGlade.setImageResource(R.drawable.group9);
        } else if (info.getIcon().equals("10")) {
            lvGlade.setImageResource(R.drawable.group10);
        }
        //组名称
        TextView tvGlade = holder.getView(R.id.tv_glade);
        tvGlade.setText(info.getName());

        //点击修改小组的头像
        llGlade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到更改的界面
                Intent intent = new Intent(context, CreateTeamActivity.class);
                intent.putExtra("flag", 100);
                String teamList = GsonUtil.listToJson(teamIcons);
                intent.putExtra("team", teamList);
                //小组名称集合
                String teamNameList = GsonUtil.listToJson(teamNames);
                intent.putExtra("teamNameList", teamNameList);
                //将班级的classNum传递给下个页面
                intent.putExtra("classnum", info.getClassnum());//班级的班级号
                intent.putExtra("teamid", info.getTeamID());//小组的teamID
                intent.putExtra("teamnum", info.getNum());//小组的num
                intent.putExtra("className", info.getName());//小组的名称
                intent.putExtra("teamicon", info.getIcon());//小组头像
                intent.putExtra("teamslogan", info.getSlogan());//小组口号
                intent.putExtra("teamtarget", info.getTaget());//小组目标
                intent.putExtra("teammusic", info.getMusic());//小组音乐
                context.startActivity(intent);
            }
        });
        //显示组员的GridView
        GridView mGv = holder.getView(R.id.mgv);
        //获取组员的学生信息
        personInfos = info.getPersonInfos();

        gvAdapter = new ClassGvAdapter(context, personInfos, R.layout.item_gv_class_manager, lp, activity);
        mGv.setAdapter(gvAdapter);
    }

    /**
     * dp转px的方法
     *
     * @return
     */
    private int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
